﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Commerce.Base.Utilities
{
    public static class Url
    {
        public static string BaseUrl
        {
            get
            {
                Uri requestUri = System.Web.HttpContext.Current.Request.Url;
                string virtualPath = HttpRuntime.AppDomainAppVirtualPath.Trim('/');
                if(!string.IsNullOrEmpty(virtualPath))
                    virtualPath += "/";
                return requestUri.Scheme + "://" + requestUri.Authority + "/" + virtualPath;
            }
        }

        public static string PathEncode(string url)
        {
            url = url.Replace("%", "¤1¤"); 
            url = url.Replace("/", "¤2¤"); 
            url = url.Replace("#", "¤3¤"); 
            url = url.Replace("?", "¤4¤"); 
            url = url.Replace("*", "¤5¤"); 
            url = url.Replace("!", "¤6¤");

            url = System.Web.HttpUtility.UrlEncode(url);
            return url.ToLower();
        }

        public static string PathDecode(string url)
        {
            url = System.Web.HttpUtility.UrlDecode(url);

            url = url.Replace("¤1¤", "%");
            url = url.Replace("¤2¤", "/");
            url = url.Replace("¤3¤", "#");
            url = url.Replace("¤4¤", "?");
            url = url.Replace("¤5¤", "*");
            url = url.Replace("¤6¤", "!");
            
            return url.ToLower();
        }
    } 
}
